module.exports = {

    index: function (req, res) {
      if (!req.session.userId) {
        return res.redirect('admin/login');
      }
      console.log(req.session.userId);
      return res.view('admin/index', {layout: 'admin/layout'});
      // var promise = List.query('select * from list', function (err, rawResult) {
      //   if (err) {return res.serverError(err)}
      //   //console.log(rawResult);
      //
      //   res.view('admin/index', {layout: 'admin/layout', data: rawResult});
      //   console.log(rawResult);
      //   return rawResult;
      // });
      // console.log();
    },

    login: function (req, res) {
      console.log(1)
      console.log(req.body);
      Admin.query('select * from admin where name = ?', req.body.username, function (err, result) {
        if (err) {return res.serverError(err);}

        console.log(result[0].password);

        if (result[0].password == req.body.password) {
          req.session.role = result[0].role;
          req.session.userId = result[0].id;
          return res.redirect('admin/index');
        }


        return res.view('admin/login', {layout: 'admin/layout_login', data: '密码错误！'});
      });
      //req.session.userId = 'userid';
      //return res.redirect('admin/index');

      //res.view('admin/login', {layout: 'admin/layout_login'});
    },

    orderAll: function (req, res) {

      return res.view('admin/order', {layout: 'admin/layout'});
    },



    logout: function (req, res) {
      if (req.session.userId) {
        req.session.userId = void(0);
        console.log(req.session.userId);
        return res.redirect('admin/login');
      }
      return res.redirect('admin/login');
    },

    viewOrder: function (req, res) {
      Order.find({id: req.body.orderId}).then(function (results) {
        if (!results) {
          res.send('no products');
        } else {
          return res.send(results);
        }
      })
    },

    //订单拆分至箱
    // splitOrderToBox: function (req, res) {
    //   var orderId = req.body.orderId;
    //   var
    //
    // },
}
